const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //班级选择----------begin
    bottom:false,
    //校区
    schoolArealist:[],
    schoolArea:'',
    schoolAreaKey:'',
    //班级类型
    classTypes: [],
    classTypesKey:'',
    //学段
    // seasons:[],
    // seasonKey:'',
    // seasonName:'',
    //年级
    grades:[],
    gradeKey:"",
    gradeName:'',
    //班级
    classs:[],
    classKey:'',
    className:'',
    viewName:'更换班级',
    isShowDetail:true,

    valName:'',
    pageNo:1,
    pageSize:50,
    stuList:[],

    ifLoad:true
  },


  didClickActionShow1(){
    var action =  this.selectComponent("#action-sheet1");
    action.onShowSingelAlert(this.data.schoolArealist,1);
   },

   didClickActionShow2(){
    var action =  this.selectComponent("#action-sheet2");
    action.onShowSingelAlert(this.data.grades,1);
   },

   didClickActionShow3(){
    var action =  this.selectComponent("#action-sheet3");
    action.onShowSingelAlert(this.data.classs,1);
   },

  didClickUp(){
    this.setData({
      isShowDetail:false
    })
  },
  actionSheetIndex1(e){
    console.log(this.data.schoolArea+'==选择的数据='+e.detail.name)
    if(this.data.schoolArea != e.detail.name){
      this.setData({
        schoolArea:e.detail.name,
        schoolAreaKey:e.detail.key
      })
      this.getClassType(1)
    }
  },
  radioChange(res){
    console.log('选择班级的数据='+JSON.stringify(res))

    this.setData({
      classTypesKey:res.detail.value
    })
    this.getClassType(3)
  },
  actionSheetIndex2(e){
    console.log('选择的数据='+e.detail.key)
    console.log('选择的数据='+e.detail.name)
    this.setData({
      gradeName:e.detail.name,
      gradeKey:e.detail.key
    })
    this.getClassType(4)
  },
  actionSheetIndex3(e){
    console.log('选择的数据='+e.detail.key)
    console.log('选择的数据='+e.detail.name)
    this.setData({
      className:e.detail.name.indexOf('班')!=-1?e.detail.name:e.detail.name+"班",
      classKey:e.detail.key
    })
  },

  //获取校区
  getSchoolArea(){
    app.api.getSchoolArea().then(rest => {
      if(rest&&rest.success){   
        if(rest.data.list!=null && rest.data.list.length > 0){
           this.setData({
            schoolArealist:rest.data.list,
            schoolArea:rest.data.list[0].name,
            schoolAreaKey:rest.data.list[0].key
           })
           this.getClassType(1)
        }
        console.log('接口数据='+JSON.stringify(rest))
      }else{
        console.log('接口数据=='+rest.errMsg)
        wx.showToast({
          title: rest.errMsg||'网络异常，请稍后重试！！',
          icon: 'none',
          duration: 2000,
          mask:true
        })
      }
    })
    .catch(r => {
      console.error(r)
      wx.showToast({
        title: '网络异常，请稍后重试！',
        icon: 'none',
        duration: 2000,
        mask:true
      })
    })
  
  },


  //获取班级类型
  getClassType(resultType){
    var that = this
    var datas
    if(resultType == 1){
      datas = {
        schoolId:this.data.schoolAreaKey,
        resultType:resultType,
        pageNo:1,
        pageSize:50,
      }
    }else if(resultType == 2){
      datas = {
        schoolId:this.data.schoolAreaKey,
        resultType:resultType,
        pageNo:1,
        pageSize:50,
        type:this.data.classTypesKey,
      }
    }else if(resultType == 3){
      datas = {
        schoolId:this.data.schoolAreaKey,
        resultType:resultType,
        pageNo:1,
        pageSize:50,
        type:this.data.classTypesKey,
        //section:this.data.seasonKey,
      }
    }else if(resultType == 4){
      datas = {
        schoolId:this.data.schoolAreaKey,
        resultType:resultType,
        pageNo:1,
        pageSize:50,
        type:this.data.classTypesKey,
        //section:this.data.seasonKey,
        grade:this.data.gradeKey,
      }
    }
    console.log('接口参数='+JSON.stringify(datas))
    app.api.getClassInfos(datas).then(rest => {
      if(rest&&rest.success){   
        if(rest.data.list!=null && rest.data.list.length > 0){    
          
          if(resultType == 1){
            this.setData({
              classTypes:rest.data.list,
             })
             this.data.classTypes[0].checked = 'true'
             this.setData({
              classTypes:rest.data.list,
              classTypesKey:rest.data.list[0].key
             })
             that.getClassType(3)
          }
          // else if(resultType == 2){
          //    this.setData({
          //     seasons:rest.data.list,
          //     seasonKey:rest.data.list[0].key,
          //     seasonName:rest.data.list[0].name,
          //    })
          //    that.getClassType(3)
          // }
          else if(resultType == 3){
            this.setData({
              grades:rest.data.list,
              gradeKey:rest.data.list[0].key,
              gradeName:rest.data.list[0].name,
            })
            that.getClassType(4)
          }else if(resultType == 4){
            this.setData({
              classs:rest.data.list,
              classKey:rest.data.list[0].key,
              className:rest.data.list[0].name.indexOf('班')!=-1?rest.data.list[0].name:rest.data.list[0].name+"班",
            })
            console.log("kkkkkkkkkkkkkk"+this.data.gradeName+this.data.className);
            if(this.data.ifLoad){
              this.setData({
                valName:this.data.gradeName+this.data.className,
                ifLoad:false
              })
            }
           
          }
        }else{
          //没有数据复位
          if(resultType == 1){
             this.setData({
              classTypes:[],
              classTypesKey:'',
              // seasons:[],
              // seasonKey:'',
              // seasonName:'',
              grades:[],
              gradeKey:'',
              gradeName:'',
              classs:[],
              classKey:'',
              className:'',
             })
          }else if(resultType == 2){
             this.setData({
              // seasons:[],
              // seasonKey:'',
              // seasonName:'',
              grades:[],
              gradeKey:'',
              gradeName:'',
              classs:[],
              classKey:'',
              className:'',
             })
            
          }else if(resultType == 3){
            this.setData({
              grades:[],
              gradeKey:'',
              gradeName:'',
              classs:[],
              classKey:'',
              className:'',
            })
           
          }else if(resultType == 4){
            this.setData({
              classs:[],
              classKey:'',
              className:'',
            })
          }
        }
        console.log('接口数据='+JSON.stringify(rest))
      }else{
        console.log('接口数据=='+rest.errMsg)
        wx.showToast({
          title: rest.errMsg||'网络异常，请稍后重试！！',
          icon: 'none',
          duration: 2000,
          mask:true
        })
      }
    })
    .catch(r => {
      console.error(r)
      wx.showToast({
        title: '网络异常，请稍后重试！',
        icon: 'none',
        duration: 2000,
        mask:true
      })
    })
  },


  bottomPop() {
  	this.setData({
  		bottom: true
    })
    //this.getSchoolArea()

  },
  cancelPopup(){
    this.hidePop()
  },

  okPopup(){
    this.hidePop()

    if(this.data.className!=''){
      this.setData({
        valName:this.data.gradeName+this.data.className,
        pageNo:1,
        dataList:[],
        totalPage:0,
        viewName:this.data.className
      })
      this.getStuList()
    }
  },


  allClass(){
    this.hidePop()
    this.setData({
      pageNo:1,
      dataList:[],
      totalPage:0,
      viewName:'全部班级',
      classKey:''
    })
    this.getStuList()
  },

  hidePop () {
    // 关闭select  .对应class #对应id
    this.selectComponent('#popup').hidePop()
  },


  getStuList(){
    wx.showLoading({
      title: '数据加载中...',
      icon: 'none',
    })
    var data = {
          pageNo:this.data.pageNo,
          pageSize:this.data.pageSize,
          classId:this.data.classKey,
          status:"1"
        }
    console.log('接口参数='+JSON.stringify(data))
    app.api.getStuList(data).then(rest => {
      wx.hideLoading()
      if(rest&&rest.success){   
        if(rest.data.studentList!=null && rest.data.studentList.length > 0){
          this.setData({
            stuList:rest.data.studentList
          })
        }
        console.log('接口数据='+JSON.stringify(rest))
      }else{
        console.log('接口数据=='+rest.errMsg)
        wx.showToast({
          title: rest.errMsg||'网络异常，请稍后重试！！',
          icon: 'none',
          duration: 2000,
          mask:true
        })
      }
    })
    .catch(r => {
      console.error(r)
      wx.showToast({
        title: '网络异常，请稍后重试！',
        icon: 'none',
        duration: 2000,
        mask:true
      })
    })
  },

  onClickCf(ret){
    console.log('接口参数sssss='+JSON.stringify(ret.currentTarget))

    wx.navigateTo({
      url: '/pages/record/exercisePrescriptionDetail?studentId='+ret.currentTarget.dataset.studentid,  //家长处方详情
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getSchoolArea()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})